// TODO: 主题若为“百威红”，请使用以下
import themeConfig from '@bud-fe/react-pc-ui/es/styles/antd-theme-red';
// TODO: 主题若为“品牌黄”，请使用以下
// import themeConfig from '@bud-fe/react-pc-ui/es/styles/antd-theme-yellow';
import { name as packageName } from './package.json';

// TODO: 【子应用】模块名。若为【独立应用】请删除
export const MODULE_NAME = 'module-1';

const PROXY_MAP = [
  {
    keys: [
      // NOTE: 如果后端API报跨域，可以在此处添加对应的路径，让dev server走proxy规避跨域问题。注意：baseUrl必须为 '/'
      // '/abi-cloud-middle-platform-user-service',
      // '/abi-cloud-middle-platform-auth-service',
      // '/abi-cloud-middle-platform-sso-server',
    ],
    target: 'https://api-gateway-dev.ab-inbev.cn',
  },
];

const proxies = PROXY_MAP.reduce((pre, cur) => {
  const curProxies = cur.keys.reduce((preV2, curV2) => {
    const proxyV2 = {
      [curV2]: { enable: true, target: cur.target },
    };
    return { ...preV2, ...proxyV2 };
  }, {});
  return { ...pre, ...curProxies };
}, {});

export default {
  hash: true,
  dropLogLevel: 'log',
  outputDir: 'dist',
  proxy: proxies,
  devServer: {
    headers: {
      'Access-Control-Allow-Origin': '*',
      'Access-Control-Allow-Methods': '*',
      'Access-Control-Allow-Headers': '*',
    },
    // TODO: 若为【子应用】时，确保与其他【子应用】端口号不重复
    port: 8801,
  },
  // TODO: 若为【子应用】时，确保与其他【子应用】端口号不重复
  devPublicPath: '//localhost:8801/',
  plugins: [
    ['build-plugin-ignore-style', { libraryName: 'antd' }],
    ['build-plugin-moment-locales', { locales: ['zh-cn'] }],
    [
      'build-plugin-antd',
      {
        disableModularImport: true,
        themeConfig: {
          ...themeConfig,
          // TODO: 其他需要修改的 antd less 变量。详见 https://ant.design/docs/react/customize-theme-cn
        },
      },
    ],
    // TODO: 【子应用】需要的ice插件。若为【独立应用】请删除
    ['build-plugin-icestark', { type: 'child', umd: true, library: MODULE_NAME }],
  ],
  lessLoaderOptions: {
    javascriptEnabled: true,
    modifyVars: {
      hack: 'true; @import "/src/styles/index.less";',
    },
  },
  modeConfig: {
    ...generateDevelopingModeConfig(['dev', 'test', 'hotfix', 'feature-1', 'feature-2']),
    ...generateProductionModeConfig(['uat', 'prod']),
  },
};

function generateDevelopingModeConfig(envList) {
  const _modeConfig = {};
  envList.forEach((env) => {
    _modeConfig[env] = {
      // 此处可以设置非 uat,prod 环境下的构建配置
      // TODO: 【子应用】的 publicPath 配置，地址为【子应用】单独部署的域名。若为【独立应用】请删除以下一行
      publicPath: `https://${MODULE_NAME}-${env}.ab-inbev.cn/`,
    };
  });
  return _modeConfig;
}

function generateProductionModeConfig(envList) {
  // abi-front-resource的oss链接改为可公开读的cdn链接 since 20230908
  const cdnOSSBaseUrl = 'https://front-resource.ab-inbev.cn';
  const buildTime = Date.now();
  const _modeConfig = {};
  envList.forEach((env) => {
    _modeConfig[env] = {
      sourceMap: 'hidden-source-map',
      publicPath: `${cdnOSSBaseUrl}/${packageName}/${env}/${buildTime}/`,
      webpackPlugins: {
        'webpack-aliyun-oss': {
          options: {
            from: ['./dist/**', '!./dist/**/*.html'],
            dist: `${packageName}/${env}/${buildTime}`,
            region: 'oss-cn-shanghai',
            accessKeyId: process.env.ACCESS_ID,
            accessKeySecret: process.env.ACCESS_SECRET,
            bucket: 'abi-front-resource',
            overwrite: true,
          },
        },
      },
    };
  });
  return _modeConfig;
}
